cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
project(service_box_network_project)

IF (MSVC)
    foreach(var
        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
        )
        if(${var} MATCHES "/MD")
            string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
        endif()
    endforeach()
	
ELSE()
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
        SET(CMAKE_CXX_FLAGS "-m64 -g -O2 -std=c++17 -Wall -Wno-deprecated-declarations -fnon-call-exceptions -Wno-unused-variable -fnon-call-exceptions -fPIC")
        set(CMAKE_CXX_COMPILER "clang++")
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        SET(CMAKE_CXX_FLAGS "-m64 -g -O2 -std=c++17 -Wall -Wno-deprecated-declarations -fnon-call-exceptions -Wno-unused-variable -fnon-call-exceptions -fPIC")
        set(CMAKE_CXX_COMPILER "g++")
    endif()
ENDIF ()

SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

INCLUDE_DIRECTORIES(
	${PROJECT_SOURCE_DIR}/../
	${PROJECT_SOURCE_DIR}/../repo/src/include
	${PROJECT_SOURCE_DIR}/../../thirdparty
	${PROJECT_SOURCE_DIR}/../../thirdparty/include
	${PROJECT_SOURCE_DIR}/../../thirdparty/kconfig/interface
	${PROJECT_SOURCE_DIR}/../../thirdparty/include/json
)

set( SRC_FILES
	${PROJECT_SOURCE_DIR}/../box/box_channel.cpp
	${PROJECT_SOURCE_DIR}/../box/box_network.cpp
	${PROJECT_SOURCE_DIR}/../box/box_network_event.cpp
	${PROJECT_SOURCE_DIR}/../box/fixed_mem_pool.cpp
	${PROJECT_SOURCE_DIR}/../box/loop/tcp_loop.cpp
	${PROJECT_SOURCE_DIR}/../box/loop/udp/udp_channel.cpp
	${PROJECT_SOURCE_DIR}/../box/loop/udp/udp_channel_loop.cpp
	${PROJECT_SOURCE_DIR}/../box/loop/udp/udp_loop.cpp
	${PROJECT_SOURCE_DIR}/../box/loop/udp/udp_socket.cpp
	${PROJECT_SOURCE_DIR}/../config/box_config.cpp
	${PROJECT_SOURCE_DIR}/../detail/box_alloc.cpp
	${PROJECT_SOURCE_DIR}/../detail/box_config_impl.cpp
	${PROJECT_SOURCE_DIR}/../detail/stack_trace_windows.cpp
	${PROJECT_SOURCE_DIR}/../detail/stack_trace_linux.cpp
	${PROJECT_SOURCE_DIR}/../detail/http_data.cpp
	${PROJECT_SOURCE_DIR}/../detail/http_base_impl.cpp
	${PROJECT_SOURCE_DIR}/../util/object_pool.cpp
	${PROJECT_SOURCE_DIR}/../util/string_util.cpp
	${PROJECT_SOURCE_DIR}/../util/os_util.cpp
	${PROJECT_SOURCE_DIR}/../util/box_debug.cpp
	${PROJECT_SOURCE_DIR}/../../thirdparty/src/http_parser/http_parser.c
)

aux_source_directory(${PROJECT_SOURCE_DIR}/../../thirdparty/src/jsoncpp SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/../../thirdparty/src/kconfig/internal SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/../../thirdparty/src/klogger/internal SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/../../thirdparty/src/knet SRC_FILES)

add_library(
	service_box_network
	${SRC_FILES}
)
target_compile_definitions(service_box_network PUBLIC
$<$<CONFIG:Debug>: -DDEBUG -D_DEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)
SET_TARGET_PROPERTIES(service_box_network PROPERTIES PREFIX "lib")
SET_TARGET_PROPERTIES(service_box_network PROPERTIES SUFFIX ".a")

IF (MSVC)
	set_target_properties(service_box_network PROPERTIES COMPILE_FLAGS "/EHa /D _WINSOCK_DEPRECATED_NO_WARNINGS /D _CRT_SECURE_NO_WARNINGS /D WIN32_LEAN_AND_MEAN /D DISABLE_SB_LOG")
ELSE ()
	set_target_properties(service_box_network PROPERTIES COMPILE_FLAGS "-DDISABLE_SB_LOG")
ENDIF ()